<template>
<div class="todo-list">
     <list-item :item="item" :index="index" v-for="(item,index) in items"></list-item>
</div>
</template>

<script>
    import listItem from '../components/item.vue'
    import bus from './../bus.js'
    export default {
        props: ["items"],
        data() {
            return {}
        },
        components: {
            listItem
        },
        mounted() {
            bus.$on("delete-todo", (index) => {
                var r = confirm('是否删除这条记录？')
                if (r == true) {
                    console.log(index)
                    this.items.splice(index, 1);
                }
            });
            bus.$on("add-todo", (item) => {
                console.log(item)
                if(item.index>=0){
                    this.items.splice(item.index, 1);
                }
                    console.log(item)
                    this.items.unshift(item);
                

                bus.$emit("add-end", 1);
            });
        },
        methods: {
            modifyItem: function(index) {
                bus.$emit("modify-item", this.item);
            }
        }
    }
</script>